package com.example.bittraffic;

import android.content.Context;
import android.graphics.Color;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MapView.LayoutParams;


public class ActivityGoogleMap extends MapActivity{
	MapView mapView;
	MapController mapCtrl;
	String locationProvider;
	LocationManager locationManager;
	Location location;
	int latitude;
	int longitude;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_activity_google_map);
		
		mapView =(MapView)findViewById(R.id.mapView);
		mapView.setBuiltInZoomControls(true);
		GeoPoint gPoint = new GeoPoint((int)(37.566912*1E6), 126978517);
		mapCtrl = mapView.getController();
		mapCtrl.setCenter(gPoint);
		mapCtrl.setZoom(16);
		
		locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		locationProvider = locationManager.getBestProvider(new Criteria(), true);
		Toast.makeText(this, "위치정보 공급자:"+locationProvider, 0).show();
		Location location = locationManager.getLastKnownLocation(locationProvider);
		
		if(location!=null){
			latitude = (int)(location.getLatitude()*1000000);
	        longitude = (int)(location.getLongitude()*1000000);
	    }
	}	

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		locationManager.requestLocationUpdates(locationProvider, 5000, 10, listener);
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		locationManager.removeUpdates(listener);
	}
	
	LocationListener listener = new LocationListener() {
		
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub
			String msg = "";
			switch(status){
			case LocationProvider.OUT_OF_SERVICE:
				Toast.makeText(ActivityGoogleMap.this, "위치 정보를 이용할 수 없습니다", 0).show();
				break;
			case LocationProvider.TEMPORARILY_UNAVAILABLE:
				Toast.makeText(ActivityGoogleMap.this, "일시적으로 이용을 할 수 없습니다", 0).show();
				break;
			case LocationProvider.AVAILABLE:
				Toast.makeText(ActivityGoogleMap.this, "위치 정보가 이용 가능합니다", 0).show();
				break;
			}
		}
		
		@Override
		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub
			Toast.makeText(ActivityGoogleMap.this, "위치 정보가 이용 가능합니다", 0).show();
		}
		
		@Override
		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub
			Toast.makeText(ActivityGoogleMap.this, "위치 정보를 이용할 수 없습니다", 0).show();
		}
		
		@Override
		public void onLocationChanged(Location location) {
			// TODO Auto-generated method stub
			mapView.removeAllViews();
			latitude = (int)(location.getLatitude()*1000000);
			longitude = (int)(location.getLongitude()*1000000);
			
			GeoPoint gPoint3 = new GeoPoint(latitude,longitude);
			ImageView image2 = new ImageView(ActivityGoogleMap.this);
			image2.setImageResource(R.drawable.ic_launcher);
			MapView.LayoutParams ip = new MapView.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,
					gPoint3,MapView.LayoutParams.CENTER);
			mapView.addView(image2,ip);
			TextView text = new TextView(ActivityGoogleMap.this);
			text.setText("현재위치");
			text.setTextSize(30);
			text.setTextColor(Color.RED);
			
			MapView.LayoutParams ip2 = new MapView.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT
					,gPoint3, MapView.LayoutParams.LEFT | MapView.LayoutParams.TOP);
			mapView.addView(text,ip2);
			mapCtrl.animateTo(gPoint3);
		}
	};
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0,1,0,"지도");
		menu.add(0,2,0,"위성");
		menu.add(0,3,0,"거리");
		menu.add(0,4,0,"현재위치");
		menu.add(0,5,0,"구라");
		menu.add(0,6,0,"반장");
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch(item.getItemId()){
		case 1:
			mapView.setSatellite(false);
			break;
		case 2:
			mapView.setSatellite(true);
			break;
		case 3:
			mapView.setStreetView(true);
			break;
		case 4:
			GeoPoint gPoint3 = new GeoPoint(latitude, longitude);			
			mapCtrl.animateTo(gPoint3);
			break;
		case 5:
			GeoPoint gPoint = new GeoPoint(37512134, 126947504);
			ImageView image1 = new ImageView(this);
			image1.setImageResource(R.drawable.ic_launcher);
			MapView.LayoutParams ip = new MapView.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,
					gPoint, MapView.LayoutParams.CENTER);
			mapView.addView(image1,ip);
			TextView text = new TextView(this);
			text.setText("OO네 집");
			text.setTextSize(30);
			text.setTextColor(Color.RED);
			
			MapView.LayoutParams ip2 = new MapView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
					gPoint, MapView.LayoutParams.LEFT | MapView.LayoutParams.TOP);
			mapView.addView(text,ip2);
			mapCtrl.animateTo(gPoint);
			break;
		case 6:
			GeoPoint gPoint2 = new GeoPoint(37476604, 126967843);
			mapCtrl.animateTo(gPoint2);
			break;
		}
		return true;
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}	
}

//public class ActivityGoogleMap extends Activity {
//TextView statusText;
//TextView result;
//String locationProvider;
//LocationManager locationManager;
//int count;
//
//@Override
//protected void onCreate(Bundle savedInstanceState) {
//	super.onCreate(savedInstanceState);
//	setContentView(R.layout.activity_activity_google_map);
//	
//	statusText = (TextView)findViewById(R.id.status);
//	result = (TextView)findViewById(R.id.result);
//	
//	locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
//	locationProvider=locationManager.getBestProvider(new Criteria(), true);
//	Toast.makeText(this, "위치 정보 공급자 :"+locationProvider, 0).show();
//	
//	Location location = locationManager.getLastKnownLocation(locationProvider);
//}
//
//@Override
//public boolean onCreateOptionsMenu(Menu menu) {
//	// Inflate the menu; this adds items to the action bar if it is present.
//	getMenuInflater().inflate(R.menu.activity_google_map, menu);
//	return true;
//}
//
//@Override
//protected void onResume() {
//	// TODO Auto-generated method stub
//	super.onResume();
//	locationManager.requestLocationUpdates(locationProvider, 5000, 10, listener);
//}
//
//@Override
//protected void onPause() {
//	// TODO Auto-generated method stub
//	super.onPause();
//	locationManager.removeUpdates(listener);
//}
//
//LocationListener listener = new LocationListener() {
//	
//	@Override
//	public void onStatusChanged(String provider, int status, Bundle extras) {
//		// TODO Auto-generated method stub
//		String msg ="";
//		switch (status) {
//		case LocationProvider.OUT_OF_SERVICE:
//			msg = "서비스 지역이 아닙니다";				
//			break;
//		case LocationProvider.TEMPORARILY_UNAVAILABLE:
//			msg = "일시적으로 위치 정보를 사용할 수 없습니다";
//			break;
//		case LocationProvider.AVAILABLE:
//			msg ="서비스 가능 지역입니다";
//			break;
//		}
//		statusText.setText(msg);
//	}
//	
//	@Override
//	public void onProviderEnabled(String provider) {
//		// TODO Auto-generated method stub
//		statusText.setText("현재 상태 : 위치정보 이용가능");
//	}
//	
//	@Override
//	public void onProviderDisabled(String provider) {
//		// TODO Auto-generated method stub
//		statusText.setText("현재 상태 : 위치정보 이용불가");
//	}
//	
//	@Override
//	public void onLocationChanged(Location location) {
//		// TODO Auto-generated method stub
//		count++;
//		StringBuffer buffer = new StringBuffer();
//		buffer.append("수신횟수 : "+count+"\r\n");
//		buffer.append("위도 : "+location.getLatitude()+"\r\n");
//		buffer.append("위도 : "+location.getLongitude()+"\r\n");
//		result.setText(buffer.toString());
//	}
//};